<template>
  <el-card class="screen-client-card" :class="{ 'sharing-active': isSharing }">
    <template #header>
      <div class="card-header">
        <span class="client-name">{{ client.name }}</span>
        <div class="header-actions">
          <el-button
            v-if="client.status === 'online'"
            type="warning"
            size="small"
            @click="$emit('save-recording', client.id)"
            class="save-recording-btn"
          >
            <el-icon><Download /></el-icon>
            保存录屏
          </el-button>
          <el-tag
            :type="client.status === 'online' ? 'success' : 'danger'"
            size="small"
          >
            {{ client.status === 'online' ? '在线' : '离线' }}
          </el-tag>
        </div>
      </div>
    </template>

    <!-- 屏幕预览区域 -->
    <div class="screen-preview" :class="{ 'active': isSharing }">
      <div v-if="!isSharing" class="preview-placeholder">
        <el-icon class="placeholder-icon"><Monitor /></el-icon>
        <div class="placeholder-text">屏幕共享未启动</div>
        <div class="performance-indicator">
          <el-tag size="small" type="info">{{ latencyText }}</el-tag>
          <el-tag size="small" type="primary">{{ fpsText }}</el-tag>
          <el-tag size="small" type="warning">{{ dataRateText }}</el-tag>
        </div>
      </div>

      <div v-else class="preview-content">
        <img
          v-if="screenSharing?.imageData"
          :src="screenSharing.imageData"
          alt="屏幕共享预览"
          class="preview-image"
          @click="$emit('view-fullscreen', client.id)"
        />
        <div v-else class="preview-loading">
          <el-icon class="loading-icon"><Loading /></el-icon>
          <div>加载中...</div>
        </div>

        <div class="preview-overlay">
          <div class="performance-stats">
            <span class="stat-item" :class="latencyClass">{{ latencyText }}</span>
            <span class="stat-item fps">{{ fpsText }}</span>
            <span class="stat-item data-rate">{{ dataRateText }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 客户端信息 -->
    <div class="client-info">
      <div class="info-grid">
        <div class="info-item">
          <span class="info-label">ID:</span>
          <span class="info-value">{{ client.id }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">IP:</span>
          <span class="info-value">{{ client.ip }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">最后心跳:</span>
          <span class="info-value">{{ client.lastHeartbeat }}</span>
        </div>
        <div v-if="screenSharing?.resolution" class="info-item">
          <span class="info-label">分辨率:</span>
          <span class="info-value">{{ screenSharing.resolution }}</span>
        </div>
        <div v-if="client.websocket_url" class="info-item">
          <span class="info-label">WebSocket:</span>
          <span class="info-value">{{ client.websocket_port }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">服务状态:</span>
          <el-tag
            :type="getServiceStatusType(client.service_status)"
            size="small"
          >
            {{ getServiceStatusText(client.service_status) }}
          </el-tag>
        </div>
      </div>
    </div>

    <!-- 控制按钮 -->
    <div class="control-buttons">
      <div class="button-row">
        <el-button
          v-if="!isSharing"
          type="success"
          size="small"
          :disabled="client.status !== 'online'"
          @click="$emit('start-sharing', client.id)"
          class="control-btn"
        >
          <el-icon><VideoPlay /></el-icon>
          开始屏幕共享
        </el-button>

        <el-button
          v-else
          type="danger"
          size="small"
          @click="$emit('stop-sharing', client.id)"
          class="control-btn"
        >
          <el-icon><VideoPause /></el-icon>
          停止屏幕共享
        </el-button>

        <el-button
          type="info"
          size="small"
          :disabled="!isSharing"
          @click="$emit('view-fullscreen', client.id)"
          class="control-btn"
        >
          <el-icon><FullScreen /></el-icon>
          全屏
        </el-button>
      </div>

      <div class="button-row">
        <el-button
          type="primary"
          size="small"
          :disabled="client.status !== 'online'"
          @click="$emit('refresh', client.id)"
          class="control-btn"
        >
          <el-icon><Refresh /></el-icon>
          刷新画面
        </el-button>

        <el-button
          type="warning"
          size="small"
          :disabled="client.status !== 'online'"
          @click="$emit('take-screenshot', client.id)"
          class="control-btn"
        >
          <el-icon><Camera /></el-icon>
          手动截图
        </el-button>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { computed } from 'vue'
import {
  Monitor, VideoPlay, VideoPause, FullScreen,
  Refresh, Loading, Download, Camera
} from '@element-plus/icons-vue'

const props = defineProps({
  client: {
    type: Object,
    required: true
  },
  screenSharing: {
    type: Object,
    default: () => ({})
  },
  stats: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits([
  'start-sharing',
  'stop-sharing',
  'view-fullscreen',
  'refresh',
  'save-recording',
  'take-screenshot'
])

// 计算属性
const isSharing = computed(() => {
  return props.screenSharing?.active || false
})

const latencyText = computed(() => {
  const latency = props.stats?.latency || 0
  return `${latency} ms`
})

const latencyClass = computed(() => {
  const latency = props.stats?.latency || 0
  if (latency < 50) return 'latency-low'
  if (latency < 150) return 'latency-medium'
  return 'latency-high'
})

const fpsText = computed(() => {
  const fps = props.stats?.fps || 0
  return `${fps} FPS`
})

const dataRateText = computed(() => {
  const dataRate = props.stats?.dataRate || 0
  return `${dataRate} KB/s`
})

// 服务状态相关
const getServiceStatusType = (status) => {
  switch (status) {
    case 'active':
      return 'success'
    case 'inactive':
      return 'danger'
    case 'warning':
      return 'warning'
    default:
      return 'info'
  }
}

const getServiceStatusText = (status) => {
  switch (status) {
    case 'active':
      return '正常'
    case 'inactive':
      return '停止'
    case 'warning':
      return '警告'
    default:
      return '未知'
  }
}
</script>

<style scoped>
.screen-client-card {
  margin-bottom: 20px;
  transition: all 0.3s ease;
  border: 1px solid #e4e7ed;
}

.screen-client-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.screen-client-card.sharing-active {
  border-color: #409EFF;
  box-shadow: 0 0 0 1px rgba(64, 158, 255, 0.2);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.client-name {
  font-weight: 600;
  color: #303133;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.save-recording-btn {
  font-size: 12px;
  padding: 4px 8px;
}

.screen-preview {
  width: 100%;
  height: 160px;
  background: #f8f9fa;
  border: 2px dashed #dee2e6;
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  overflow: hidden;
}

.screen-preview.active {
  background: #e8f5e8;
  border-color: #67C23A;
  border-style: solid;
}

.preview-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #909399;
}

.placeholder-icon {
  font-size: 32px;
  margin-bottom: 8px;
}

.placeholder-text {
  font-size: 14px;
  margin-bottom: 10px;
}

.preview-content {
  position: relative;
  width: 100%;
  height: 100%;
}

.preview-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.preview-image:hover {
  transform: scale(1.02);
}

.preview-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #409EFF;
}

.loading-icon {
  font-size: 24px;
  margin-bottom: 8px;
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.preview-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0,0,0,0.7));
  padding: 10px;
}

.performance-stats {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.performance-indicator {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.stat-item {
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: bold;
  color: white;
}

.stat-item.latency-low {
  background: rgba(103, 194, 58, 0.8);
}

.stat-item.latency-medium {
  background: rgba(230, 162, 60, 0.8);
}

.stat-item.latency-high {
  background: rgba(245, 108, 108, 0.8);
}

.stat-item.fps {
  background: rgba(64, 158, 255, 0.8);
}

.stat-item.data-rate {
  background: rgba(111, 66, 193, 0.8);
}

.client-info {
  background: #f8f9fa;
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 15px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 12px;
  min-width: 0;
}

.info-label {
  color: #909399;
  font-weight: 500;
  font-size: 11px;
  white-space: nowrap;
}

.info-value {
  color: #303133;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  word-break: break-all;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.control-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.button-row {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
}

.control-btn {
  flex: 1;
  font-size: 12px;
  min-width: 0;
  white-space: nowrap;
  padding: 8px 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .screen-preview {
    height: 140px;
  }

  .performance-indicator {
    gap: 4px;
  }

  .stat-item {
    font-size: 10px;
    padding: 1px 4px;
  }

  .button-row {
    flex-wrap: wrap;
  }

  .control-btn {
    flex: 1 1 auto;
    min-width: 80px;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .header-actions {
    width: 100%;
    justify-content: flex-end;
  }
}
</style>
